import * as actionTypes from './actionTypes'
import axios from 'axios'
import { fromJS } from 'immutable'

const changeSearchList = (data, totalPage) => {
  return {
    type: actionTypes.CHANGE_SEARCH_LIST,
    data: fromJS(data),
    totalPage
  }
}

export function changeToFocusAction() {
  return {
    type: actionTypes.CHANGE_TO_FOCUS
  }
}

export function changeToBlurAction() {
  return {
    type: actionTypes.CHANGE_TO_BLUR
  }
}

export function getSearchList() {
  return dispatch => {
    axios
      .get('/api/headerSearchList.json')
      .then(res => {
        let data = res.data
        let totalPage = Math.ceil(data.data.length / 10)
        if (data.success) {
          dispatch(changeSearchList(data.data, totalPage))
        }
      })
      .catch(() => {
        console.log('err')
      })
  }
}

export function changeToEnter() {
  return {
    type: actionTypes.CHANGE_TO_ENTER
  }
}
export function changeToLeave() {
  return {
    type: actionTypes.CHANGE_TO_LEAVE
  }
}

export function changePage(page) {
  return {
    type: actionTypes.CHANGE_PAGE,
    page
  }
}